package com.seek.service.imp;

import com.seek.core.BaseServiceImpl;
import com.seek.dao.AuthMetricDao;
import com.seek.model.entity.TAuthMetric;
import com.seek.service.AuthMetricService;
import com.seek.util.CommonUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * Created by weh on 2017/7/4.
 */
@Repository("authMetricService")
public class AuthMetricServiceImpl extends BaseServiceImpl<TAuthMetric> implements
    AuthMetricService {

  @Autowired
  public void setDao(AuthMetricDao authMetricDao) {
    super.setDao(authMetricDao);
  }

  public AuthMetricDao getDao() {
    return (AuthMetricDao) super.getDao();
  }

  @Override
  public List<TAuthMetric> queryAuthMetricBySql(Map<String, Object> map) {
    List<Object> params = new ArrayList<>();
    List<TAuthMetric> result = new ArrayList<>();
    try {
      StringBuffer sql = new StringBuffer(
          "select a.id,a.company_id as companyId,a.auth_rule_id as authRuleId,a.min_metric as minMetric,");
      sql.append(
          "a.max_metric as maxMetric,a.auth_value as authValue,a.auth_pass as authPass, fail_remark as failRemark,a.remark,a.deleted ,");
      sql.append(
          "b.auth_prdouct as authProduct,b.auth_dimension as authDimension,b.auth_desc as  authDesc ");
      sql.append("from t_auth_metric a, t_auth_rule b where a.auth_rule_id = b.id");
      if (map.containsKey("authProduct")) {
        sql.append(" and b.auth_prdouct= ?");
        params.add(map.get("authProduct"));
      }
      if (map.containsKey("authDimension")) {
        sql.append(" and b.auth_dimension= ?");
        params.add(map.get("authDimension"));

      }
      if (map.containsKey("companyId")) {
        sql.append(" and a.company_id= ?");
        params.add(map.get("companyId"));
      }
      sql.append(" and a.deleted=0 ");

      List<Map> mapBySql = this.getDao()
          .findMapBySql(sql.toString(), params.toArray(new Object[params.size()]));
      if (CollectionUtils.isNotEmpty(mapBySql)) {
        for (Map obj : mapBySql) {
          TAuthMetric metric = new TAuthMetric();
          metric.setId(CommonUtil.getLongValue(obj.get("id")));
          metric.setCompanyId(CommonUtil.getLongValue(obj.get("companyId")));
          metric.setAuthRuleId(CommonUtil.getLongValue(obj.get("authRuleId")));
          metric.setMinMetric(String.valueOf(obj.get("minMetric")));
          metric.setMaxMetric(String.valueOf(obj.get("maxMetric")));
          metric.setAuthValue(String.valueOf(obj.get("authValue")));
          metric.setAuthPass(CommonUtil.getIntValue(obj.get("authPass")));
          metric.setFailRemark(String.valueOf(obj.get("failRemark")));
          metric.setRemark(String.valueOf(obj.get("remark")));
          metric.setDeleted(CommonUtil.getIntValue(obj.get("deleted")));
          metric.setAuthProduct(String.valueOf(obj.get("authProduct")));
          metric.setAuthDimension(String.valueOf(obj.get("authDimension")));
          metric.setAuthDesc(String.valueOf(obj.get("authDesc")));
          result.add(metric);
        }

      }

      return result;
    } catch (Exception e) {
      e.printStackTrace();
    }

    return null;
  }
}
